﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Edgar.Entities.Characteristics;
using Edgar.Entities.MasterData;
using System.Windows;

namespace Edgar.Frontend.Wpf.ViewModels
{
    public class SkillViewModel : INotifyPropertyChanged
    {
        private readonly Rulebook _rulebook;
        private readonly CharacterSkill _characterSkill;

        public SkillViewModel(Rulebook rulebook, CharacterSkill characterSkill, int attributeStep)
        {
            _rulebook = rulebook;
            _characterSkill = characterSkill;
            AttributeStep = attributeStep;
        }

        public string Name { get { return _characterSkill.Name; } }

        public int Rank
        {
            get { return _characterSkill.Rank; } // TODO
            set
            {
                _characterSkill.Rank = value;
                NotifyPropertyChanged("Rank");
                NotifyPropertyChanged("Step");
                NotifyPropertyChanged("Dice");
                NotifyPropertyChanged("KnowledgeSkillPointsLeft");
                NotifyPropertyChanged("CraftingSkillPointsLeft");
            }
        }

        public string AttributeName
        {
            get
            {
                switch (Category)
                {
                    case "Wissensfertigkeit": return "Wahrnehmung";
                    case "Kunstfertigkeit": return "Charisma";
                    case "Allgemeine Fertigkeit": return GetAttributeForGeneralSkill(Name); // TODO
                    default: return "UNKNOWN CATEGORY!";
                }
            }
        }

        private string GetAttributeForGeneralSkill(string skillName)
        {
            var skill = _rulebook.Skills.GeneralSkills.Find(x => x.Name == skillName);
            if (skill == null) return "UNABLE TO FIND GENERAL SKILL";
            return skill.Attribute;
        }

        public int AttributeStep { get; set; }
        public int Step { get { return Rank > 0 ? AttributeStep + Rank : 0; } }
        public string Dice { get { return Rank > 0 ? _rulebook.GetDice(Step) : ""; } }

        public string Category
        {
            get
            {
                return _characterSkill.Category;
            }
        }

        public Visibility HiddenIfNoAttribute { get { return string.IsNullOrEmpty(AttributeName) ? Visibility.Hidden : Visibility.Visible; } }

        #region Implementing INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }

        #endregion
    }
}
